package Structure;

/**
 * @Author: ylc
 * @Description:
 * @Date Created in 2022-06-06 16:51
 */
public class GraphGenerator {
    // matrix为N*2的矩阵
    // [weight,from上的点,to上的点]
    public static Graph generateGraph(int[][] matrix){
        Graph graph = new Graph();
        for (Integer i = 0; i < matrix.length; i++) {
            int weight = matrix[i][0];
            int from = matrix[i][1];
            int to = matrix[i][2];
            if(!graph.nodes.containsKey(from)){
                graph.nodes.put(from, new Node(from));
            }
            if(!graph.nodes.containsKey(to)){
                graph.nodes.put(to, new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge newEdge = new Edge(weight, fromNode, toNode);
            fromNode.nexts.add(toNode);
            fromNode.out++;
            toNode.in++;
            fromNode.edges.add(newEdge);
            graph.edges.add(newEdge);
        }
        return graph;
    }

}
